package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
//import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.article.vo.StrategySVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {


    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail detail, String tags) {
        //目的地id
        Strategy strategy = strategyService.get(detail.getStrategy().getId());
        Destination dest = strategy.getDest();
        detail.setDest(dest);
        //作者id
        //摘要
        String content = detail.getStrategyContent().getContent();
        if (content.length() > 200) {
            detail.setSummary(content.substring(0, 200));
        } else {
            detail.setSummary(content);
        }
        //是否是国外
        Destination destination = destinationService.getToasts(dest.getId()).get(0);
        detail.setIsabroad(destination.getId() != 1);


        if (detail.getId() != null) {
            strategyDetailMapper.updateByPrimaryKey(detail);
            //内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
        } else {
            //创建时间
            detail.setCreateTime(new Date());
            strategyDetailMapper.insert(detail);
            //内容
            StrategyContent strategyContent = detail.getStrategyContent();
            strategyContent.setId(detail.getId());
            strategyContentMapper.insert(strategyContent);
        }
        strategyDetailMapper.deleteRelation(detail.getId());
        if (StringUtils.hasLength(tags)) {
            String[] tag = tags.split(",");
            for (String t : tag) {
                StrategyTag tag1 = new StrategyTag();
                tag1.setName(t);
                strategyTagService.saveOrUpdate(tag1);
                strategyDetailMapper.insertRelation(detail.getId(), tag1.getId());
            }
        }
    }


    @Override
    public List<StrategyDetail> getViewNumTop3(Long destId) {
        return strategyDetailMapper.selectViewNumTop3(destId);
    }

    @Override
    public void updateReplyNum(Long id, int replynum) {
        strategyDetailMapper.updateReplyNum(id, replynum);
    }

    @Override
    public List<StrategyDetail> getAbroadCds() {
        return strategyDetailMapper.getAbroadCds();
    }

    @Override
    public List<StrategyDetail> getUnabroadCds() {
        return strategyDetailMapper.getUnabroadCds();
    }

    @Override
    public void updateStatis(StrategySVO strategySVO) {
        strategyDetailMapper.updateStatis(strategySVO);
    }



/*    @Override
    public void updateStatisVO(StrategyStatisPersistenceVO vo) {
        strategyDetailMapper.updateStatisVO(vo);
    }*/
}
